#!/bin/sh

DIR_CONFIG="/etc/ucarp-multi"
DIR_REFERENCE="${DIR_CONFIG}/reference"
DIR_USR_SHARE__REAL="/usr/share/ucarp-multi"
DIR_USR_SHARE__REF="${DIR_REFERENCE}${DIR_USR_SHARE__REAL}"
DIR_VIP_AVAILABLE="${DIR_CONFIG}/vip-available"
DIR_VIP_ENABLED="${DIR_CONFIG}/vip-enabled"

FILE_CONFIG__REF="${DIR_REFERENCE}${DIR_CONFIG}/ucarp.conf"


#ask for parameters


#list the interfaces
echo "The available interfaces are:"
ifconfig -a -s | sed 's/ .*//g' | sed 's/.*:.*//g' | uniq
echo "On which one interface do you want to create the subinterface?"
read INTERFACE
ifconfig $INTERFACE
if [ $? -ne 0 ]; then 
	echo "$INTERFACE does not appear to be a valid interface"
	exit 1
fi


echo "Which subinterface do you want to use? (any integer greater or equal than 1)"
read SUBIF_NUMBER

SUBIF_STR="${INTERFACE}_${SUBIF_NUMBER}"
SUBIF="${INTERFACE}:${SUBIF_NUMBER}"


PASSWORD_FILE="${DIR_VIP_AVAILABLE}/${SUBIF_STR}.passwd"
CONFIG_FILE="${DIR_VIP_AVAILABLE}/${SUBIF_STR}.conf"


# in case it is not already present, create the script directory
[ -d "${DIR_USR_SHARE__REAL}" ] || mkdir ${DIR_USR_SHARE__REAL}


# then move an adapt the ifup and ifdown scripts
FILES[0]="vip-down_"
FILES[1]="vip-up_"
for i in 0 1
do
	# copy the ifup,ifdown file
	NEW_FILE="${DIR_USR_SHARE__REAL}/${FILES[$i]}${SUBIF_STR}"
	cp -f "${DIR_USR_SHARE__REF}/${FILES[$i]}" ${NEW_FILE}

	#set the approriate permissions
	chmod 755 ${NEW_FILE}

	#adapt the values
	sed -i "s/INTERFACE/${SUBIF}/g" ${NEW_FILE}
done



echo "Which ipv4 address would you like use? (eg 192.168.1.3)"
read IP4_ADDRESS

echo "Which network mask would you like to use? (default: 255.255.255.0)"
read IP4_MASK
if [ -z $IP4_MASK ]; then
	IP4_MASK="255.255.255.0"
fi



echo "Which shared secret would you like to use to authenticate and to encrypt communication between peers?"
read PASSWORD
#PASSWORD="vip-eth01"

echo "${PASSWORD}" > ${PASSWORD_FILE}
# securing the password file
chmod 700 ${PASSWORD_FILE} && chown root:root ${PASSWORD_FILE}


echo "Will the current running computer be the MASTER for that interface? [yes/no] (default:yes)"
read UCARP_MASTER
if [ -z $UCARP_MASTER ]; then
	UCARP_MASTER="yes"
fi

echo "Which priority would you like to set?  The lower the more important it is. (default:100)"
read UCARP_PRIORITY
if [ -z $UCARP_PRIORITY ]; then
	UCARP_PRIORITY="100"
fi


echo "How often should this ucarp daemon exchange messages? [integer in seconds] (default:1)"
read UCARP_ADV
if [ -z $UCARP_ADV ]; then
	UCARP_ADV="1"
fi

echo " What is the VID of this group? (default:55)"
read UCARP_VID
if [ -z $UCARP_VID ]; then
	UCARP_VID="55"
fi


echo "Should the DEBUG of that interface be enabled? [0/1] (default:0)"
read DEBUG_VALUE
if [ -z $DEBUG_VALUE ]; then
	DEBUG_VALUE="0"
fi





# creating the configuration file
cat ${FILE_CONFIG__REF} > ${CONFIG_FILE}
chmod +x ${CONFIG_FILE}

#fills its values
echo "Filling configuration values"

# UCARP_MASTER
NAME[0]="_VALUE__UCARP_MASTER"
VALUE[0]=$UCARP_MASTER

# UCARP_INTERFACE
NAME[1]="_VALUE__INTERFACE"
VALUE[1]=$INTERFACE

# UCARP ADDR PHY
NAME[2]="_VALUE__UCARP_ADDR_PHY"
VALUE[2]=`ifconfig $INTERFACE | grep "inet addr" | sed 's/inet addr://g' | sed 's/ Bcast.*//g'`


# UCARP VID
NAME[3]="_VALUE__UCARP_VID"
VALUE[3]=$UCARP_VID

# UCARP PRIORITY
NAME[4]="_VALUE__UCARP_ADVSKEW"
VALUE[4]=$UCARP_PRIORITY

# UCARP ADD VIRT
NAME[5]="_VALUE__ADDR_VIR"
VALUE[5]=$IP4_ADDRESS


# frequency of exchange messages
NAME[6]="_VALUE__EXCHANGE_MESSAGE_DELAY"
VALUE[6]=$UCARP_ADV

# DEBUG
NAME[7]="_VALUE__DEBUG_VALUE"
VALUE[7]=$DEBUG_VALUE

# PASSWORD_FILE
NAME[8]="_VALUE__UCARP_PASSWD_FILE"
VALUE[8]=`echo "${PASSWORD_FILE}"| sed 's/\\//\\\\\//g'`

#IFUP FILE
NAME[9]="_VALUE__UCARP_UP_INT_SCRIPT"
VALUE[9]=`echo "${DIR_USR_SHARE__REAL}/${FILES[1]}${SUBIF_STR}" | sed 's/\\//\\\\\//g'` 

#IFDOWN FILE
NAME[10]="_VALUE__UCARP_DOWN_INT_SCRIPT"
VALUE[10]=`echo "${DIR_USR_SHARE__REAL}/${FILES[0]}${SUBIF_STR}" | sed 's/\\//\\\\\//g'` 



for i in 0 1 2 3 4 5 6 7 8 9 10; do
	echo " - ${NAME[$i]} = ${VALUE[$i]}"
	sed -i "s/${NAME[$i]}/${VALUE[$i]}/g" ${CONFIG_FILE}
done


# enable this vip
echo "Would you like to enable this vip? [y/n]"
read enable
if [ $enable != "y" ];
then
	exit
fi
echo "Enabling the ${SUBIF} sub-interface"
ln -sf "${CONFIG_FILE}" "${DIR_VIP_ENABLED}/"


# impact on /etc/network/interfaces
FILE_INTERFACES="/etc/network/interfaces"
# warn before
echo "WARNING: i am about to modify the ${FILE_INTERFACES} file. Here are the change I am going to perform:"

echo "Do you want to continue? [y/n]"
read continue
if [ $continue != "y" ]; 
then
	echo " - TO DO: Please manually update that file." 
else 

	echo "auto ${SUBIF}" >> ${FILE_INTERFACES}
	echo "iface ${SUBIF} inet static" >> ${FILE_INTERFACES}
	echo "   address ${IP4_ADDRESS}" >> ${FILE_INTERFACES}
	echo "   netmask ${IP4_MASK}" >> ${FILE_INTERFACES}
fi


echo "Would you like me to restart your 'networking' and 'ucarp-multi' services?"
read RESTART
if [ -z $RESTART ]; then
	RESTART="n"
fi
if [ $RESTART != "y" ]; then
	echo " - TO DO: Please manually restart the 'networking' and the ucarp-multi services."
	echo "   You have to run:"
	echo " /etc/init.d/networking restart ; /etc/init.d/ucarp-multi restart"
else
	# restart networking
	/etc/init.d/networking restart

	# restart ucarp-multi
	/etc/init.d/ucarp-multi restart
fi
exit
